package com.leyou.item.web;

import com.leyou.item.dto.SpecGroupDTO;
import com.leyou.item.dto.SpecParamDTO;
import com.leyou.item.service.SpecGroupService;
import com.leyou.item.service.SpecParamService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author ShenMin
 * @date 2021/7/16 16:03
 * @description
 */
@RestController
@RequestMapping("/spec")
public class SpecController {
    @Autowired
    SpecGroupService specGroupService;

    @Autowired
    SpecParamService specParamService;

    @GetMapping("/groups/of/category")
    public ResponseEntity<List<SpecGroupDTO>> listSpecGroupByCategoryId(@RequestParam("id") Long cid) {
        return ResponseEntity.ok(specGroupService.listSpecGroupByCategoryId(cid));
    }

    @GetMapping("/params")
    public ResponseEntity<List<SpecParamDTO>> listSpecParam(
            @RequestParam(value = "categoryId", required = false) Long cid,
            @RequestParam(value = "groupId", required = false) Long gid,
            @RequestParam(value = "searching", required = false) Boolean isSearch
    ) {
        return ResponseEntity.ok(specParamService.listSpecParam(cid, gid, isSearch));
    }

    @PostMapping("/group")
    public ResponseEntity<Void> addSpecGroup(@RequestBody SpecGroupDTO specGroupDTO){
        specGroupService.addSpecGroup(specGroupDTO);
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }

    @PutMapping("/group")
    public ResponseEntity<Void> updateSpecGroup(@RequestBody SpecGroupDTO specGroupDTO){
        specGroupService.updateSpecGroup(specGroupDTO);
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }

    @PostMapping("/param")
    public ResponseEntity<Void> addSpecParam (@RequestBody SpecParamDTO specParamDTO){
        specParamService.addSpecParam(specParamDTO);
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }

    @PutMapping("/param")
    public ResponseEntity<Void> updateSpecParam (@RequestBody SpecParamDTO specParamDTO){
        specParamService.updateSpecParam(specParamDTO);
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }

    @GetMapping("/list")
    public ResponseEntity<List<SpecGroupDTO>> listSpecList(@RequestParam("id") Long cid){
        return ResponseEntity.ok(specGroupService.listSpecList(cid));
    }

}
